package controllers;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class IndexController extends HttpServlet {

    /* HTTP GET Request */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String address = "/index.jsp";
        String loggedin = "false";
        for (Cookie cookie : request.getCookies()) {
            System.out.println("Cookie");
            if (cookie.getName().equals("status")) {
                System.out.println("Status: " + cookie.getValue());
                if (cookie.getValue().length() > 0) {
                    loggedin = "true";
                }
            }
        }
        System.out.println(loggedin);
        HttpSession session = request.getSession(true);
        session.setAttribute("loggedin", loggedin);
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}